#include <gtk/gtk.h>
#include <string.h>
#define N 100

char *filename;
char fname[N];
GtkWidget *FileWin;

/*************************获取需要发送的文件名至filename**************************/
static void file_ok_sel( GtkWidget *w,GtkFileSelection *fs )
{
  int i;
  const gchar *fn;
  fn= gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs));//gchar类型的文件名

  for(i=0; i<strlen(fn); i++)
  fname[i] = *(fn+i);
  fname[i] = '\0';
  filename = fname;

  g_print("%s\n",filename);
  gtk_widget_hide(FileWin);
}

/************************文件选择框************************************/
void  FileSelection(int argc,char* argv[])
{
  gtk_init(&argc,&argv);  //调用gtk_init初始化GTK库

  FileWin = gtk_file_selection_new ("File selection");

  g_signal_connect(G_OBJECT(FileWin), "destroy",G_CALLBACK(gtk_main_quit), NULL);//关闭窗口时结束函数

  g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(FileWin)->ok_button),"clicked", G_CALLBACK(file_ok_sel), (gpointer)FileWin);//点击ok按钮调用file_ok_sel（）函数

  
  g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(FileWin)->cancel_button), "clicked",G_CALLBACK(gtk_widget_destroy), G_OBJECT (FileWin));//点击取消按钮关闭当前窗口

  gtk_file_selection_set_filename (GTK_FILE_SELECTION(FileWin),NULL); //选择框中的默认文件名

  gtk_widget_show(FileWin);
  gtk_main();

}

